import { organizationQueryList } from '@/api'
import getTreeData from '@/utils/jsonTree'
const state = {
  organization: null
}

const mutations = {
  CONSTANT_ORANIZATION: (state, payload) => {
    state.organization = payload
  }
}

const actions = {
  getOrgData({ commit }) {
    return new Promise((resolve, reject) => {
      organizationQueryList().then(response => {
        const { data } = response
        const res = data.map(item => ({
          label: item.name,
          ...item
        }))
        const treeData = getTreeData(res)
        if (!data) {
          reject('获取机构数据失败')
        }
        commit('CONSTANT_ORANIZATION', treeData)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

